/*
调用组件封装
*/
import store from 'store'
function request({url, data, method="GET", contenType=1}) {
	let header = {
		'content-type': contenType === 1 ? 'application/json' : 'application/x-www-form-urlencoded',
		'platform': 'app',
		'token':uni.getStorageSync('token') || ''
	}
	
	let type = uni.getSystemInfoSync().uniPlatform;
	let baseUrl = '';
	if(type == 'web') {
		baseUrl = "/api/sys";
	}else {
		const accountInfo = wx.getAccountInfoSync();
		if(accountInfo.miniProgram.envVersion=='release'){
		    baseUrl = "https://zznc.ahyzg.com/api/sys";
		}else baseUrl = "https://zzndevelop.ahyzg.com/api/sys"
		 // baseUrl = "https://zznc.ahyzg.com/api/sys";
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url:baseUrl + url,
			data,
			method,
			header,
			responseType: contenType !== 1 ? "arraybuffer" : "",
			success:res => {
				if(res.statusCode === 200) {
					if(res.data.code === 300 || res.data.code === 301) {
						uni.showToast({
							icon:'none',
							title:'你还没有登录，请在我的界面登录！',
							duration:3000
						});
						store.commit('storeTokenOut');
						store.commit('storeLogout');
					}else {
						resolve(res.data)
					}
				} else if(res.statusCode === 405) {
					uni.showToast({
						icon:'none',
						title:'请求方法错误',
						duration:1500
					});
				} else {
					uni.showToast({
						icon:'none',
						title:'请求错误' + res.statusCode,
						duration:1500
					});
				}
			},
			fail: (err) => {
				uni.showToast({
					icon:'none',
					title:err.errMsg,
					duration:1500
				});
				reject(err)
			}
		})
	})
}

export default { request }